package com.turbo.boot.crawler.spider.worker.impl.quota;

import com.alibaba.fastjson.JSON;
import com.turbo.boot.crawler.spider.worker.BaseTurboApiCrawler;
import com.turbo.utils.net.HttpHelper;
import com.xuxueli.crawler.rundata.RunData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 波罗的海交易指数
 *
 * @author wuyn
 * @since 2024/4/17
 */
@Slf4j
@Service("balticExchangeCrawler")
public class BalticExchangeCrawler extends BaseTurboApiCrawler<BalticExchangeCrawlerPageParser.PageVo> {

    private static final HttpHelper httpHelper = new HttpHelper();


//    public boolean build() {
//        xxlCrawlerBuilder.setUrls("https://blacksun-api.balticexchange.com/api/ticker");
//        return true;
//    }


    @Override
    public String remoteCall(RunData runData) {
        String url = runData.getUrl();
        try {
            return httpHelper.get(url);
        } catch (Exception e) {
            log.error("", e);
        }
        return null;
    }

    @Override
    public void parse(String result) {
        List<BalticExchangeCrawlerPageParser.PageVo> pageVos = JSON.parseArray(result, BalticExchangeCrawlerPageParser.PageVo.class);
        for (BalticExchangeCrawlerPageParser.PageVo pageVo : pageVos) {
            try {
                getPageParser().parse(pageVo);
            } catch (Exception e) {
                log.error("", e);
            }
        }
    }


//    @Override
//    protected BaseTurboCrawlerPageParser<BalticExchangeCrawlerPageParser.PageVo> getPageParser() {
//        return new BalticExchangeCrawlerPageParser();
//    }


    public static void main(String[] args) {
        BalticExchangeCrawler balticExchangeCrawler = new BalticExchangeCrawler();
        balticExchangeCrawler.crawl();
    }
}
